Imports System.Data
Imports System.Data.SqlClient
Imports Conexao_liion

Partial Class Moeda
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache)

        btnAlterar.Enabled = False
        btnExcluir.Enabled = False

        CarregaGVW()

    End Sub

    Private Sub CarregaGVW()

        Using theSqlConnection As New SqlConnection(Conexao_liion.theConnectionString)

            Dim mydatatable As New DataTable
            Dim theQueryString As String = "SELECT A.CodigoDaMoeda, A.NomeDaMoeda, A.SiglaDaMoeda, " & _
                        "A.SimboloDaMoeda, B.Sigla2DoPais, A.VigenciaDaMoeda " & _
                        "FROM Moeda A, Pais B " & _
                        "WHERE A.CodigoDoPaisISO = B.CodigoDoPaisISO " & _
                        "ORDER BY A.NomeDaMoeda"

            Dim theSqlCommand As SqlCommand = theSqlConnection.CreateCommand()
            theSqlCommand.CommandText = theQueryString

            Try
                theSqlConnection.Open()
                Dim theSqlDataReader As SqlDataReader = theSqlCommand.ExecuteReader()

                mydatatable.Load(theSqlDataReader)
                gvwData.DataSource = mydatatable
                gvwData.DataBind()
                theSqlDataReader.Close()
            Catch ex As Exception
                'theSqlDataReader.Close()
                lblMsg.Text = "Msg DB: " & ex.Message
            End Try

        End Using

    End Sub

    Protected Sub gvwData_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvwData.PageIndexChanging

        gvwData.PageIndex = e.NewPageIndex
        gvwData.DataBind()

    End Sub

    Protected Sub gvwData_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvwData.SelectedIndexChanged

        'If Not Page.IsPostBack Then
        btnAlterar.Enabled = True
        btnExcluir.Enabled = True
        'End If

    End Sub



    Protected Sub btnIncluir_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnIncluir.Click

        Session.Add("strOperacao", "I") ' Incluir

        Session.Add("strCodigoDaMoeda", "")

        Response.Redirect("MoedaData.aspx")

    End Sub

    Protected Sub btnAlterar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAlterar.Click

        Session.Add("strOperacao", "A") ' Alterar

        Session.Add("strCodigoDaMoeda", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(0).Text))

        Response.Redirect("MoedaData.aspx")

    End Sub

    Protected Sub btnExcluir_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExcluir.Click

        Session.Add("strOperacao", "E") ' Excluir

        Session.Add("strCodigoDaMoeda", HttpUtility.HtmlDecode(gvwData.SelectedRow.Cells(0).Text))

        Response.Redirect("MoedaData.aspx")

    End Sub

    Protected Sub btnVoltar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnVoltar.Click

        Response.Redirect("Default.aspx")

    End Sub

End Class
